package com.mall.manager.userlogin.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.WebUtils;

import com.mall.security.UserAuthenticationToken;
import com.mall.manager.user.service.ManageUserService;
import com.mall.manager.userlogin.service.ManagerUserLoginService;
import com.mall.untils.MD5Util;
import com.mall.untils.Page;

/**登陆
 * @author Administrator
 *
 */
@Controller
public class ManagerUserLoginController {
	 Logger logger = LoggerFactory.getLogger(ManagerUserLoginController.class);
	 @Resource ManagerUserLoginService LoginService;
	 @Resource ManageUserService userService;
	 
	 /**首页登陆页面
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/login")
		public String login(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
			return "login";
		}
	 /**登陆
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/manager_user_login",method=RequestMethod.POST)
	 public String manager_user_login(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");		
		Map<String, Object> userMap=LoginService.manager_user_login(paraMap);
		if(userMap!=null&&userMap.size()>0){ 
			HttpSession session = request.getSession();
			session.setAttribute("user", userMap);
			List<Map<String, Object>>  powerList1=LoginService.getPowerbyId(Integer.parseInt( String.valueOf(userMap.get("id"))),1);
			List<Map<String, Object>>  powerList2=LoginService.getPowerbyId(Integer.parseInt( String.valueOf(userMap.get("id"))),2);
			List<Map<String, Object>>  powerList3=LoginService.getPowerbyId(Integer.parseInt( String.valueOf(userMap.get("id"))),3);
			List<Map<String, Object>>  power=LoginService.getAllpower(Integer.parseInt( String.valueOf(userMap.get("id"))));
			session.setAttribute("powerList1", powerList1);
			session.setAttribute("powerList2", powerList2);
			session.setAttribute("powerList3", powerList3);	
			UserAuthenticationToken token = new UserAuthenticationToken(Integer.parseInt( String.valueOf(userMap.get("id"))),String.valueOf(userMap.get("user_name")), String.valueOf(userMap.get("pwd")), power, null);
			org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
			subject.login(token);
			return "index";
		}else{
			return "login";
		}				 
	    	
	    }
	 
	 
	 /**
	     * 分页查询用户信息
	     * @param request
	     * @param response
	     * @param modelMap
	     * @return
	     */
		@SuppressWarnings("unchecked")
		@RequestMapping(value = "/manager_user_loginvalate")    
	    public String manager_user_loginvalate(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {   
	    	Page page= userService.manager_user_list(request);    	
	     	if(null!=page){
	     		List<Map<String,Object>> list = (List<Map<String,Object>>)page.getResult();
	     		for(int i=0;i<list.size();i++){
	     			list.get(i).put("pwd",MD5Util.convertMD5(String.valueOf(list.get(i).get("pwd"))));
	     		}
		     	modelMap.addAttribute("manager_user_list", list);
		     	int totalpagecount = page.getTotalPageCount();
		     	modelMap.addAttribute("totalpagecount", totalpagecount);
		     	int currentpageno = page.getCurrentPageNo();
		     	modelMap.addAttribute("currentpageno", currentpageno);
		     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
						.getCurrentPageNo() - 1;
		     	modelMap.addAttribute("previouspage", previouspage);
		     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
						.getTotalPageCount() : page.getCurrentPageNo() + 1;
				modelMap.addAttribute("nextpage", nextpage); 
				List<Integer> showNumList = page.getShowNumList();
				modelMap.addAttribute("pagenumlist", showNumList);
	     	}
	         	     	
	        return "user/manager_user_list";
	    }
		/**访问首页
		 * @param request
		 * @param response
		 * @param modelMap
		 * @return
		 */
		@RequestMapping(value = "/indexpc")
		public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
			return "index";
		}
		 
		 /**退出登陆
		 * @param request
		 * @param response
		 * @param modelMap
		 * @return
		 */
		@RequestMapping(value = "/logout")
		 public String logout(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
				HttpSession sesion = request.getSession();
				sesion.removeAttribute("user");
				return "login"; 
		 }
}
